/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.page;

import hu.glassy.web.panel.header.HeaderPanel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The parent of all web pages in the application
 *
 * @author molnar.alexander@sonrisa.hu
 */
public abstract class WebPageTemplate extends WebPage implements BaseWebPage
{

  private static final long serialVersionUID = 3027213151185968694L;
  // Common logger for all web pages
  protected static final Logger logger = LoggerFactory.getLogger(WebPage.class);
  /// Store the previous panel
  private Panel lastPanel;
  /// Store the current panel
  private Panel currentPanel;
  private final WebMarkupContainer contentWrapper;

  /**
   * Konstructor
   *
   * @param parameters
   */
  public WebPageTemplate(final PageParameters parameters)
  {
    super(parameters);
    add(new Label("title", getPageTitleModel()));
    add(getHeaderPanel("headerPanel"));

    contentWrapper = new WebMarkupContainer("contentWrapper");
    contentWrapper.setOutputMarkupId(true);
    currentPanel = getContentPanel(CONTENT_ID, parameters);
    contentWrapper.add(currentPanel);
    add(contentWrapper);
  }

  public IModel<String> getPageTitleModel()
  {
    return new ResourceModel("homepage.welcome");
  }

  public Panel getHeaderPanel(final String panelId)
  {
    return new HeaderPanel(panelId);
  }

  public Panel getContentPanel(final String panelId, final PageParameters parameters)
  {
    return new EmptyPanel(panelId);
  }

  public void setContentPanel(final Panel panel, final AjaxRequestTarget target)
  {
    lastPanel = currentPanel;
    currentPanel = panel;
    contentWrapper.addOrReplace(panel);
    target.add(contentWrapper);
  }

  public void goBack(final AjaxRequestTarget target)
  {
    if (isGoBackAllowed())
    {
      setContentPanel(lastPanel, target);
    }
  }

  public boolean isGoBackAllowed()
  {
    return lastPanel != null;
  }

  @Override
  public boolean isVersioned()
  {
    return false;
  }
}
